package com.homer.gps.track.helper;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Environment;

import com.homer.gps.track.R;

public class UsefullStaff {
	
	public enum Dir{IMAGE, EXPORT}
	
	public static String getFormatedDateAndTime(Date datetime, String dateformat){
		DateFormat dateFormat = new SimpleDateFormat(dateformat);
		return dateFormat.format(datetime);
	}
	
	public static String getElapsedTime(Date startDate){
		long elapsedTime = (new Date().getTime() - startDate.getTime()); 
	    String format = String.format("%%0%dd", 2);  
	    elapsedTime = elapsedTime / 1000;  
	    String seconds = String.format(format, elapsedTime % 60);  
	    String minutes = String.format(format, (elapsedTime % 3600) / 60);  
	    String hours = String.format(format, elapsedTime / 3600);  
	    String time =  hours + ":" + minutes + ":" + seconds;  
	    return time;  
	}
	
	public static int getPlaceTypeImageResource(int placeType){
		switch (placeType) {
		case 0:
			return R.drawable.place_type_home;
		case 1:
			return R.drawable.place_type_customer;
		case 2:
			return R.drawable.place_type_shop;
		case 3:
			return R.drawable.place_type_other;
		default:
			return R.drawable.place_type_other;
		}
	}
	
	public static int getRouteTypeImageResource(int routeType){
		switch (routeType) {
		case 0:
			return R.drawable.route_type_personal;
		case 1:
			return R.drawable.route_type_bussiness;
		case 2:
			return R.drawable.route_type_other;
		default:
			return R.drawable.route_type_other;
		}
	}
	
	public static String getRouteTypeString(Context context, int routeType){
		String[] routeTypes = context.getResources().getStringArray(R.array.route_types);
		return routeTypes[routeType];
	}
	
	public static String getPlaceTypeString(Context context, int placeType){
		String[] placeTypes = context.getResources().getStringArray(R.array.place_types);
		return placeTypes[placeType];
	}
	
	public static void restart(Activity activity, Intent intent) {
		if (intent == null) {
			intent = new Intent(activity, activity.getClass());
		}
		// finish the activity
		// start the activity
		activity.startActivity(intent);
		activity.overridePendingTransition(0,0);
		activity.finish();
	}
	
	public static void checkGpsSettings(final Context context){
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
	    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
	        AlertDialog.Builder builder = new AlertDialog.Builder(context);
	        builder.setTitle(R.string.gps_not_found_title);  // GPS not found
	        builder.setMessage(R.string.gps_not_found_message); // Want to enable?
	        builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialogInterface, int i) {
	            	context.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
	            }
	        });
	        builder.setNegativeButton(R.string.no, null);
	        builder.create().show();
	        return;
	    }
	}
	
	public static String getExternalStorageDir(Context context, Dir dir, String name) {
		String subApp;
		if (dir.equals(Dir.IMAGE)) {
			subApp =  context.getString(R.string.images_folder);
		}else {
			subApp =  context.getString(R.string.exports_folder);
		}

		String rootPath = getExternalStorageAppRootDir(context);
		File noMedia = new File(rootPath + File.separator + ".nomedia");
		if (!noMedia.exists()) {
			try {
				noMedia.getParentFile().mkdirs();
				noMedia.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return rootPath + File.separator + subApp + File.separator + name;
	}

	public static String getExternalStorageAppRootDir(Context context) {
		return Environment.getExternalStorageDirectory() + File.separator + context.getString(context.getApplicationInfo().labelRes);
	}
}
